package com.dylan.历史.算法.排序;

import com.dylan.历史.util.LogUtil;

import java.util.Arrays;

/**
 * Created by Dylan on 2018/10/23.
 */
public class 基础_排序_冒泡排序 {

    public static void main(String[] args) {
        LogUtil.info(Arrays.toString(bubbleSort(new int[]{3, 6, 2, 7, 2, 6, 8, 0})));
    }

    /**
     * 冒泡排序算法 O(n^2）
     */
    static int[] bubbleSort(int[] arr) {
        int len = arr.length;
        boolean sorted;
        for (int i = 0; i < len - 1; i++) {
            sorted = false;
            for (int j = 0; j < len - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    sorted = true;
                }
            }
            if (!sorted) {
                break;
            }
        }
        return arr;
    }
}
